package com.tedu.weibo.controller;

import com.tedu.weibo.mapper.CommentMapper;
import com.tedu.weibo.pojo.dto.CommentDTO;
import com.tedu.weibo.pojo.entity.Comment;
import com.tedu.weibo.pojo.entity.User;
import com.tedu.weibo.pojo.vo.CommentVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Api(tags="评论模块")
@RestController
@RequestMapping("/v1/comment")
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;

    @ApiOperation("发表评论模块")
    @PostMapping("/insert")
    public int commentInsert(@RequestBody CommentDTO commentDTO,HttpSession session) {
        System.out.println(commentDTO);
        User user = (User)session.getAttribute("user");
        if(user==null) {
            System.out.println("未登录，评论失败！");
             return 2;
        }else {
            Comment comment = new Comment();
            comment.setContent(commentDTO.getContent());
            comment.setCreated(new Date());
            comment.setUserId(user.getId());
            comment.setWeiboId(commentDTO.getWeiboId());
            commentMapper.insert(comment);
            System.out.println("评论成功");
            return 1;
        }

    }

    @ApiOperation("获取微博评论列表模块")
    @GetMapping("/selectByWeiboId")
    public List<CommentVO> selectByWeiboId(int id){
        return commentMapper.selectByWeiboId(id);
    }



}
